package com.example.user.customviewdemo.layerlist;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.user.customviewdemo.R;

import butterknife.Bind;
import butterknife.ButterKnife;

public class LayerListActivity extends AppCompatActivity {

    @Bind(R.id.progressbar)
    ProgressBar progressbar;
    @Bind(R.id.textview)
    TextView textview;

    private Handler handler;
    private float width;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layer_list);
        ButterKnife.bind(this);
        handler = new Handler();

        width = getResources().getDisplayMetrics().widthPixels;

        new Thread(new Runnable() {
            int i = 0;

            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progressbar.setProgress(i++);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            textview.setTranslationX(i*(width - 160)/100);
                            textview.setText(i+"%");
                        }
                    });
                    if (i == 101) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                textview.setText("完成");
                                textview.setTranslationX(width - 160);
                            }
                        });
                        break;
                    }

                }
            }
        }).start();
    }
}
